package com.woniu.inlet.web.controller;

import com.woniu.exception.CouponException;
import com.woniu.exception.UserCouponException;
import com.woniu.inlet.web.fo.CouponFo;
import com.woniu.outlet.mysql.po.UserCoupon;
import com.woniu.service.CouponService;
import com.woniu.service.command.AddCouponCommand;
import com.woniu.service.command.CouponPayCommand;
import com.woniu.service.command.DistributeCommand;
import com.woniu.service.command.UpdateUserCouponCommand;
import com.woniu.utils.JwtTokenUtil;
import com.woniu.utils.LoginPass;
import com.woniu.utils.ResultResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@Api("优惠券功能模块测试")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @PostMapping("/coupon/add")
    @ApiOperation("管理员添加优惠券")
    public Object addCoupon(@RequestBody CouponFo couponFo) {
        try {
            AddCouponCommand command = new AddCouponCommand(couponFo);
            command.excute();
            return new ResultResponseUtil<>(200, "优惠券添加成功");
        } catch (Exception e) {
            if (e instanceof CouponException)
                return new ResultResponseUtil<>(500, e.getMessage());
            else
                return new ResultResponseUtil<>(500, "优惠券系统维护中");
        }
    }

    @GetMapping("/coupon/distribute")
    @ApiOperation("管理员分发优惠券")
    @LoginPass
    public Object distribute(Integer couponId, @RequestParam() String token, @RequestParam List<Integer> userIds) {
        try {
            DistributeCommand command = new DistributeCommand(couponId, userIds);
            command.excute();
            return new ResultResponseUtil<>(200, "优惠券分发成功");
        } catch (Exception e) {
            if (e instanceof UserCouponException)
                return new ResultResponseUtil<>(400, e.getMessage());
            else
                e.printStackTrace();
            return new ResultResponseUtil<>(500, "系统维护中");
        }
    }

    @GetMapping("/coupon/pay")
    @ApiOperation("支付时查询优惠券")
    public Object couponPay(@RequestParam("userId") Integer userId, @RequestParam("money") BigDecimal money) {
        try {
            CouponPayCommand command = new CouponPayCommand(userId, money);
            List<UserCoupon> userCoupons = command.excute();
            return userCoupons;
        } catch (Exception e) {
            if (e instanceof UserCouponException)
                return new ResultResponseUtil<>(200, e.getMessage());
            else
                return new ResultResponseUtil<>(500, "系统维护中");
        }
    }

    //支付模块使用优惠券支付
    @GetMapping("/coupon/use")
    @ApiOperation("使用优惠券后修改状态")
    public void useCoupon(@RequestParam("userCouponId") Integer userCouponId) {
        try {
            UpdateUserCouponCommand command = new UpdateUserCouponCommand(userCouponId);
            command.excute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*用户查询优惠券
     * 1.查询状态未使用，已过期的
     * 2.根据本地时间判断过期 已经过期的修改状态
     * 3.最后将结果返回给用户
     */
/*    @GetMapping("/coupon/{userId}")
    @ApiOperation("用户查询优惠券状态")
    public void checkCouppn(@PathVariable("userId") Integer userId) {

    }*/

    //根据id查询优惠券用户关联表
    @GetMapping("/coupon/usercoupon/{id}")
    @ApiOperation("根据用户优惠券id查询优惠券详情")
    public UserCoupon getUsercoupnById(@PathVariable("id") Integer id) {
        UserCoupon usercoupon = couponService.getUsercouponById(id);
        return usercoupon;
    }

    //管理员获得登录token
    @GetMapping("/coupon/admintoken")
    @ApiOperation("管理员登录获取的token")
    public Object adminToken() {
        try {
            String sign = JwtTokenUtil.createSign("admin", "admin");
            return new ResultResponseUtil<>(200, "获取token成功", sign);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultResponseUtil<>(500, "系统维护中，请稍后重试");
        }
    }
}

